package com.tzxs.pay.main.onlineaut;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityOnlineAutBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.onlineaut.broadcast.AutFinishBroadCast;
import com.tzxs.pay.main.onlineaut.fragment.OnAutFragment;
import com.tzxs.pay.main.onlineaut.fragment.OnlineAutListFragment;
import com.tzxs.pay.main.onlineaut.navigator.FinishNavigator;
import com.tzxs.pay.util.StatusBarColorSetter;

import java.util.Objects;

/**
 * 在线预授权
 */
public class OnlineAutActivity extends BaseActivity<ActivityOnlineAutBinding> implements FinishNavigator {
    private OnAutFragment onAutFragment;
    private OnlineAutListFragment onlineAutListFragment;
    private AutFinishBroadCast broadCast;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_online_aut;
    }

    //初始化fragment
    private void initFragment() {
        //最好不要用這個方式創建
        onAutFragment = new OnAutFragment();
        onlineAutListFragment = new OnlineAutListFragment();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        //沉浸toolbar
        dataBinding.autToolbar.setTitle("");
        setSupportActionBar(dataBinding.autToolbar);
        dataBinding.autToolbar.setNavigationOnClickListener(v -> {
            closeKeyBoard();
            onBackPressed();
        });
        dataBinding.autToolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.toolbar_menu_search) {
                Intent intent = new Intent(OnlineAutActivity.this, OnlineAutSActivity.class);
                startActivity(intent);
                return true;
            }
            return false;
        });
        initFragment();
        dataBinding.onlineAutVp.setOffscreenPageLimit(1);
        dataBinding.onlineAutVp.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return position == 0 ? onAutFragment : onlineAutListFragment;
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                if (position == 0) {
                    return "在线预授权";
                } else if (position == 1) {
                    return "预授权列表";
                }
                return super.getPageTitle(position);
            }
        });
        dataBinding.tab.addTab(dataBinding.tab.newTab().setText("在线预授权"));
        dataBinding.tab.addTab(dataBinding.tab.newTab().setText("预授权列表"));
        dataBinding.tab.setupWithViewPager(dataBinding.onlineAutVp);
        dataBinding.onlineAutVp.setOffscreenPageLimit(1);
        initBroadCase();
    }

    //关闭软键盘
    private void closeKeyBoard() {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        View view = getWindow().peekDecorView();
        if (view != null) {
            if (Objects.requireNonNull(manager).isActive()) {
                manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            closeKeyBoard();
        }
        return super.onKeyDown(keyCode, event);
    }

    //初始化广播
    private void initBroadCase() {
        broadCast = new AutFinishBroadCast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AutFinishBroadCast.ACTION_FINISH);
        registerReceiver(broadCast, intentFilter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (broadCast != null) {
            unregisterReceiver(broadCast);
            broadCast = null;
        }
    }

    @Override
    public void clear() {
        if (onAutFragment != null) {
            onAutFragment.clear();
        }
    }
}
